package com.controller;

import com.POJO.Grade;
import com.POJO.GradeRequest;
import com.POJO.StudentClass;
import com.POJO.Subject;
import com.POJO.User;
import com.service.GradeRequestService;
import com.service.GradeService;
import com.service.StudentClassService;
import com.service.SubjectService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Package com.controller
 * @Date 2024-05-23 12:02
 * @Description 学生管理的控制器类，处理前端关于学生相关请求
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentClassService studentClassService;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private UserService userService;

    @Autowired
    private GradeRequestService gradeRequestService;

    @Autowired
    private SubjectService subjectService;

    /**
     * 获取学生的所有成绩
     * @param studentId 学生ID
     * @return 包含学生成绩和班级信息的视图
     */
    @GetMapping("/{studentId}")
    public ModelAndView getStudentById(@PathVariable("studentId") int studentId) {
        List<StudentClass> studentClasses = studentClassService.getStudentClassByStudentId(studentId);
        List<Grade> grades = gradeService.getGradesByStudentId(studentId);
        ModelAndView modelAndView = new ModelAndView("studentGrades");
        modelAndView.addObject("studentClasses", studentClasses);
        modelAndView.addObject("grades", grades);
        return modelAndView;
    }

    /**
     * 跳转到学生首页
     * @param request HTTP请求
     * @return studentHome视图
     */
    @GetMapping("/")
    public ModelAndView toStudentHome(HttpServletRequest request) {
        User student = (User) request.getSession().getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("studentHome");
        modelAndView.addObject("student", student);
        return modelAndView;
    }

    /**
     * 跳转到编辑用户信息页面
     * @param request HTTP请求
     * @return editUserInfo视图
     */
    @GetMapping("/edit")
    public ModelAndView editUserInfo(HttpServletRequest request) {
        User student = (User) request.getSession().getAttribute("user");
        ModelAndView modelAndView = new ModelAndView("editUserInfo");
        modelAndView.addObject("student", student);
        return modelAndView;
    }

    /**
     * 跳转到成绩修改申请页面
     * @param request HTTP请求
     * @return studentGradeRequest视图
     */
    @GetMapping("/request")
    public ModelAndView studentGradeRequest(HttpServletRequest request) {
        User student = (User) request.getSession().getAttribute("user");
        List<GradeRequest> gradeRequests = gradeRequestService.getGradeRequestsByStudentId(student.getUserId());
        List<Subject> subjects = subjectService.getAllSubjects(); // 获取所有科目
        ModelAndView modelAndView = new ModelAndView("studentGradeRequest");
        modelAndView.addObject("gradeRequests", gradeRequests);
        modelAndView.addObject("subjects", subjects);
        modelAndView.addObject("student", student);
        return modelAndView;
    }

    /**
     * 修改用户信息
     * @param student 学生对象
     * @return 重定向到学生主页
     */
    @PostMapping("/update")
    public String updateUserInfo(@ModelAttribute("student") User student) {
        userService.updateUser(student);
        return "redirect:/student/";
    }
}
